package com.jiamao.goplay.ui.login;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.gyf.immersionbar.OnKeyboardListener;
import com.jiamao.goplay.util.KeyInputUtils;
import com.jyn.vcview.VerificationCodeView;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.LoginPhoneNextSmsLayoutBinding;
import com.jiamao.goplay.ui.login.vm.LoginViewModel;
import com.youth.banner.util.LogUtils;


/**
 * 一个MVVM模式的登陆界面
 */
public class LoginPhoneSMSFragment extends BaseMVVMFragment<LoginPhoneNextSmsLayoutBinding, LoginViewModel> {
    //ActivityLoginBinding类是databinding框架自定生成的,对应activity_login.xml

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.login_phone_next_sms_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public LoginViewModel initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(LoginViewModel.class);
    }



    @Override
    public void initViewObservable() {


        viewModel.reTimeSmsCode();
        viewModel.uc.reBtnGetCode.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //pSwitchObservable是boolean类型的观察者,所以可以直接使用它的值改变密码开关的图标
                if (aBoolean) {
                    //密码可见
                    //在xml中定义id后,使用binding可以直接拿到这个view的引用,不再需要findViewById去找控件了
                    binding.deLoginSign.setBackgroundResource(R.drawable.bg_login_btn_ok);
                    binding.deLoginSign.setTextColor(Color.WHITE);

                } else {
                    //密码不可见
                    binding.deLoginSign.setBackgroundResource(R.drawable.bg_login_btn_default);
                    binding.deLoginSign.setTextColor(Color.parseColor("#d0d0d0"));
                }
            }
        });


    }

    @Override
    public void initData() {
        super.initData();

        ImmersionBar.with(this). keyboardEnable(true).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).
                 keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)  //单独指定软键盘模式
                .setOnKeyboardListener(new OnKeyboardListener() {    //软键盘监听回调，keyboardEnable为true才会回调此方法
                    @Override
                    public void onKeyboardChange(boolean isPopup, int keyboardHeight) {
                        LogUtils.e("isPopup "+isPopup);  //isPopup为true，软键盘弹出，为false，软键盘关闭
                    }
                }).init();

        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("");
        binding.getRoot().findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });


        VerificationCodeView verificationCodeView =  binding.getRoot().findViewById(R.id.verificationView);
        verificationCodeView.setOnCodeFinishListener(new VerificationCodeView.OnCodeFinishListener() {
            @Override
            public void onTextChange(View view, String content) {

            }

            @Override
            public void onComplete(View view, String content) {
                        viewModel.loginSendSMS(getArguments().getString("phone"),content);

            }
        });

        Bundle mBundle = getArguments();
        if (mBundle != null) {
            String phone = mBundle.getString("phone");
            viewModel.smsPhone.set("+86 "+phone);
        }

        new Handler().postDelayed(new Runnable(){
            public void run() {
                binding.verificationView.requestFocus();
                KeyInputUtils.ShowKeyboard(binding.verificationView);
            }
        }, 500);

    }


}
